<?php


namespace App\Http\Controllers\Admin\Gift;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

//礼物说明背景
class DescBgController extends BaseController
{
    /**
     * @api {post} /gift/desc-bg 新增礼物说明背景
     *
     * @apiGroup 礼物-说明背景
     *
     * @apiParam {string} bg_img_url 背景图url，必须
     * @apiParam {string} status 状态：1：启用；0：禁用，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function add(Request $request)
    {
        $param = $request->only('bg_img_url', 'status');
        $v = app('validator')->make($param, [
            'bg_img_url' => 'required|url|max:255',
            'status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_gift')->addOrEditDescBg($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /gift/desc-bg 编辑礼物说明背景
     *
     * @apiGroup 礼物-说明背景
     *
     * @apiParam {Int} id 要编辑的记录id，必须
     * @apiParam {string} bg_img_url 背景图url，必须
     * @apiParam {string} status 状态：1：启用；0：禁用，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'bg_img_url', 'status');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'bg_img_url' => 'required|url|max:255',
            'status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_gift')->addOrEditDescBg($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {delete} /gift/desc-bg 删除礼物说明背景
     *
     * @apiGroup 礼物-说明背景
     *
     * @apiParam {Int|string} ids 要删除的记录id，多个用英文逗号连接，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function del(Request $request)
    {
        $ids = $request->ids;

        if (empty($ids)) throw new BasicException(0, '请选择要删除的记录');

        $ids = explode(',', $ids);

        $res = app('model_gift_desc_bg')->whereIn('id', $ids)->update(['is_delete' => 1]);
        if ($res) return Response::sendData([]);
        throw new BasicException(0, '删除失败');
    }

    /**
     * @api {get} /gift/desc-bg 礼物说明背景列表
     *
     * @apiGroup 礼物-说明背景
     *
     * @apiParam {Int} status 状态：-1（默认）：不限制；1：启用；0：禁用
     * @apiParam {Int} page 第几页
     * @apiParam {Int} page_size 每页显示条数，默认20
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 1, 序号
    "img_url": "http://jfssssls.jpg", 图片url
    "status": 1, 状态，1：启用；0：禁用
    "created_at": "2021-08-11 17:51:03" 创建时间
    },
    {
    "id": 2,
    "img_url": "http://jfls.jpg",
    "status": 1,
    "created_at": "2021-08-11 18:17:09"
    }
    ],
    "first_page_url": "http://local.qipaoadmin/gift/desc-bg?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.qipaoadmin/gift/desc-bg?page=1",
    "next_page_url": null,
    "path": "http://local.qipaoadmin/gift/desc-bg",
    "per_page": 20,
    "prev_page_url": null,
    "to": 2,
    "total": 2
    }
    }
     */
    public function list(Request $request)
    {
        $param = $request->only('status', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'status' => 'integer|in:-1,0,1',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $status = $param['status'] ?? -1;
        $pageSize = $param['page_size'] ?? 20;

        $where = [];
        if ($status > -1) {
            $where[] = ['status', '=', $status];
        }

        $list = app('model_gift_desc_bg')
            ->select('id', 'img_url', 'status', 'created_at')
            ->where('is_delete', 0)
            ->where($where)
            ->paginate($pageSize);
        return Response::sendData($list);
    }
    
    /**
     * @api {get} /gift/desc-bg/all 获取所有礼物说明背景
     *
     * @apiGroup 礼物-说明背景
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 1, 序号
    "img_url": "http://jfssssls.jpg" 背景图url
    },
    {
    "id": 2,
    "img_url": "http://jfls.jpg"
    }
    ]
    }
     */
    public function all()
    {
        $list = app('model_gift_desc_bg')
            ->select('id', 'img_url')
            ->where('is_delete', 0)
            ->where('status', 1)
            ->get();
        return Response::sendData($list);
    }
}